<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar
      title="注册 / 登录"
      left-arrow
      @click-left="$router.back()"
      class="nav"
    />
    <!-- 登录表单 -->
    <van-form
      :show-error="false"
      :show-error-message="false"
      validate-first
      ref="formRef"
      @submit="loginBtn"
      @failed="errorForm"
    >
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        class="iconfont"
        icon-prefix="icon"
        left-icon="shouji"
        :rules="formRules.mobile"
      />
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        class="iconfont"
        icon-prefix="icon"
        left-icon="yanzhengma"
        :rules="formRules.code"
      >
        <!-- 获取验证码按钮 -->
        <template #button>
          <van-button
            size="small"
            round
            class="verify-btn"
            @click.prevent="gainNote"
            v-if="countDownShow"
            :loading="SendSmsLoadingShow"
            >获取验证码</van-button
          >
          <van-count-down
            v-else
            :time="1000 * 5"
            format="ss s"
            @finish="countDownShow = true"
          />
        </template>
      </van-field>
      <div class="login-btn">
        <van-button block type="info">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { getLoginApi, getSendSmsApi } from '@/api/user.js'

export default {
  name: 'Login',
  data () {
    return {
      user: {
        mobile: '13911111111',
        code: '246810'
      },
      // 登录表单效验规则
      formRules: {
        mobile: [
          { required: true, message: '请输入手机号码' },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '手机号码格式错误' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      // 控制验证码倒计时的显示与隐藏
      countDownShow: true,
      // 控制验证码按钮加载状态
      SendSmsLoadingShow: false
    }
  },
  methods: {
    loginBtn () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      // 发起登录请求
      getLoginApi(this.user)
        .then((res) => {
          this.$toast.success('登录成功')
          // 存储token值到vuex里
          this.$store.commit('setUser', res.data.data)
          // 跳转我的页面(暂时用后退替代)
          this.$router.back()
        })
        .catch(() => {
          this.$toast.fail('登录失败,请检查手机号或验证码是否正确')
        })
    },
    // 表单效验不通过触发
    errorForm (val) {
      if (val.errors[0]) {
        this.$toast({
          message: val.errors[0].message,
          position: 'top'
        })
      }
    },
    // 点击获取验证码触发
    gainNote () {
      // 效验手机号是否正确
      this.$refs.formRef
        .validate('mobile')
        .then(() => {
          // 显示按钮加载状态
          this.SendSmsLoadingShow = true
          // 发送验证码请求
          getSendSmsApi(this.user.mobile).then(() => {
            // 发送验证码成功后隐藏发送按钮
            this.countDownShow = false
            // 隐藏按钮加载状态
            this.SendSmsLoadingShow = false
          }).catch(() => {
            this.$toast({
              message: '发送太频繁，请稍后重试',
              position: 'top'
            })
            // 隐藏按钮加载状态
            this.SendSmsLoadingShow = false
          })
        })
        .catch((err) => {
          this.$toast({
            message: err.message,
            position: 'top'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>
// icon样式
.iconfont {
  font-size: 21px;
}
// 导航栏背景颜色
.nav {
  background-color: #3296fa;
}
// 导航栏标题颜色
/deep/.van-nav-bar__title {
  color: #fff;
}
// 导航栏箭头颜色
/deep/.van-icon-arrow-left:before {
  color: #fff;
}
.van-field {
  align-items: center;
  height: 52px;
}
// 输入框字体的样式
/deep/.van-field__control {
  font-size: 14px;
  margin-left: 10px;
}
// 登录按钮样式
.login-btn {
  margin: 16px;
  .van-button {
    background-color: #67b0ff;
    border: none;
    border-radius: 5px;
  }
}
// 获取验证码按钮的样式
.verify-btn {
  width: 80px;
  height: 25px;
  background-color: #ecebec;
  font-size: 11px;
  color: #666;
}
</style>
